package com.xiangwei.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.stereotype.Service;

import com.xiangwei.dao.GodMapper;
import com.xiangwei.dao.QrcodeMapper;
import com.xiangwei.entity.God;
import com.xiangwei.service.IGodService;
import com.xiangwei.util.Common;

/**
 *
 * @brief 客户表Service处理类
 */
@Service
public class GodServiceImpl implements IGodService {
	private static Logger logger = LoggerFactory.getLogger(GodServiceImpl.class);
    @Resource
    private GodMapper godMapper;
    @Resource
	private QrcodeMapper qrcodeMapper;

	@Override
	public int selectGodByDay(Map<String, Object> map) {
		return godMapper.findGodCountInDay(map);
	}

	@Override
	public int selectGobCountById(String id) {
		if(!Common.isEmptyString(id)){
			logger.error("method selectGobCountById 传入参数为空");
			return 0;
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("productId", id);
		return godMapper.getGodCount(map);
	}

	@Override
	public God selectByOpenid(String openid,String productId) {
		// TODO Auto-generated method stub
		if(!Common.isEmptyString(openid)){
			logger.error("method selectByOpenid 传入参数为空");
			return null;
		}
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("productId", productId);
		map.put("openid", openid);
		String tableName = "qlm_protal_god_"+productId;
		int count = qrcodeMapper.existTable(tableName);
		if(count <= 0){
			qrcodeMapper.createGodTable(tableName);
			return null;
		}
		return godMapper.selectByOpenid(map);
	}

	@Override
	public int insertSelective(Map<String,Object> map) {
		if(Common.isNullOrEmpty(map)){
			logger.error("method insertSelective 传入参数为空");
			return 0;			
		}	
		String productId = (String) map.get("productId");
		String tableName = "qlm_protal_god_"+productId;
		int count = qrcodeMapper.existTable(tableName);
//		System.out.println("count="+count);
		if(count <= 0){
			qrcodeMapper.createGodTable(tableName);
		}
		return godMapper.insertGod(map);
	}

	
	
}
